package com.gz.purchase.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gz.purchase.R;
import com.gz.purchase.library.pojo.MoveInStorageItemInfo;
import com.gz.purchase.library.pojo.StorageCheckProductInfo;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 移库入库确认  界面移库单适配器
 * author wanner1991
 */
public class MoveInStorageConfirmAdapter extends BaseAdapter{
    private Context mContext;
    private List<MoveInStorageItemInfo> mDataList;
    private OnButtonYesOrNoClickListener myListener;

    public MoveInStorageConfirmAdapter(Context mContext, List<MoveInStorageItemInfo> mDataList, OnButtonYesOrNoClickListener myListener) {
        this.mContext = mContext;
        this.mDataList = mDataList;
        this.myListener = myListener;
    }

    @Override
    public int getCount() {
        return mDataList.size();
    }

    @Override
    public Object getItem(int i) {
        return mDataList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if (view == null){
            view = LayoutInflater.from(mContext).inflate(R.layout.move_in_storage_item,null);
            holder = new ViewHolder();
            holder.tv_storage_name = (TextView) view.findViewById(R.id.tv_storage_name);
            holder.tv_create_time = (TextView) view.findViewById(R.id.tv_create_time);
            holder.list = (LinearLayout) view.findViewById(R.id.list_products);
            holder.btn_no = (Button) view.findViewById(R.id.btn_no);
            holder.btn_yes = (Button) view.findViewById(R.id.btn_yes);

            view.setTag(holder);
        }else{
            holder = (ViewHolder) view.getTag();
        }

        final  MoveInStorageItemInfo info = mDataList.get(i);
        holder.tv_storage_name.setText("来自"+info.getStorage_from_name());
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd HH:mm");
        holder.tv_create_time.setText(format.format(new Date(info.getCreate_at()*1000)));

        // 动态添加商品条目
        List<StorageCheckProductInfo> products = info.getProductInfoList();
        holder.list.removeAllViews();
        if (products != null) {
            for (int j = 0; j < products.size(); j++) {
                StorageCheckProductInfo product = products.get(j);
                View productItem = LayoutInflater.from(mContext).inflate(R.layout.move_in_storage_product_item, null);
                ((TextView) productItem.findViewById(R.id.tv_product_name)).setText(product.getProduct_name());
                ((TextView) productItem.findViewById(R.id.tv_product_unit_and_num)).setText("1" + product.getProduct_unit() + "*" + product.getProduct_num());
                ((TextView) productItem.findViewById(R.id.tv_product_shop_name)).setText(product.getShop_name());
                holder.list.addView(productItem);
            }
        }

        holder.btn_no.setTag(info);
        holder.btn_no.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MoveInStorageItemInfo info = (MoveInStorageItemInfo) view.getTag();
                if (info != null){
                    myListener.onClickNoBtn(info);
                }
            }
        });

        holder.btn_yes.setTag(info);
        holder.btn_yes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MoveInStorageItemInfo info = (MoveInStorageItemInfo) view.getTag();
                if (info != null){
                   myListener.onClickYesBtn(info);
                }
            }
        });


        return view;
    }

    class ViewHolder{
        TextView tv_storage_name;
        TextView tv_create_time;
        LinearLayout list ;
        Button btn_no;
        Button btn_yes;
    }

    public interface OnButtonYesOrNoClickListener{
        void onClickYesBtn(MoveInStorageItemInfo info);
        void onClickNoBtn(MoveInStorageItemInfo info);
    }
}
